/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   27.03.2010
 * @version SVN: $Id: TimestampableListener.java 46 2010-03-30 22:28:13Z christian-kerl@web.de $
 */
package org.cakis.server.model;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

/**
 * TimestampableListener
 *
 * @author Christian Kerl
 */
public class TimestampableListener {
    private static final String TIMEZONE_UTC = "UTC";

    @PrePersist
    public void prePersist(Timestampable object) {
        object.setCreatedAt(getNowAsUTCDate());
    }

    @PreUpdate
    public void preUpdate(Timestampable object) {
        object.setUpdatedAt(getNowAsUTCDate());
    }

    private Date getNowAsUTCDate() {
        return Calendar.getInstance(TimeZone.getTimeZone(TIMEZONE_UTC)).getTime();
    }
}
